package com.fecean.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class TestController {


    @GetMapping("/test")
    public String test(){
        return "TestController.test" + Instant.now().toEpochMilli();
    }

    @SentinelResource(value = "getOrder", blockHandler = "getOrderBlockExceptionHandler")
    @GetMapping("/getOrder")
    public String getOrder(){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            log.error("getOrder", e);
        }
        return "getOrder";
    }

    public String getOrderBlockExceptionHandler(BlockException be){
        log.error("getOrderBlockExceptionHandler", be);
        return "getOrderBlockExceptionHandler";
    }
}
